﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Person;
using BusinessObjects.Person;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Person
{
    public class SqlServerAccountingObjectDao : IAccountingObjectDao
    {
        public List<AccountingObject> GetAccountingObjects()
        {
            string procedures = "Get_All_AccountingObjects";

            return Db.ReadList(procedures, true, Make);
        }

        public List<AccountingObject> GetAccountingObjectsByBranch(string restaurantId)
        {
            string procedures = "Get_All_AccountingObjects_ByBranch";

            object[] parms = { "@RestaurantId", restaurantId };

            return Db.ReadList(procedures, true, Make);
        }

        public List<AccountingObject> GetAccountingObjectsByPage(int currentPage)
        {
            string procedures = "Get_Paged_AccountingObjects";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};

            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<AccountingObject> GetAccountingObjectsByPageAndBranch(int currentPage, string restaurantId)
        {
            string procedures = "Get_Paged_AccountingObjects_ByBranch";

            object[] parms = {  "@WhereClause", " RestaurantId = '" + restaurantId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};

            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<AccountingObject> GetAccountingObjectsByPageAndBranch(int currentPage, string restaurantId, bool isCustomer, bool isVendor, bool isEmployee)
        {
            string procedures = "Get_Paged_AccountingObjects_ByBranch";
            int _isCustomer = 0;
            int _isVendor = 0;
            int _isEmployee = 0;

            if (isCustomer == true) _isCustomer = 1;
            else _isCustomer = 0;

            if (isVendor == true) _isVendor = 1;
            else _isVendor = 0;

            if (isEmployee == true) _isEmployee = 1;
            else _isEmployee = 0;

            object[] parms = {  "@WhereClause", " RestaurantId = '" + restaurantId + "'" +
                                                " AND IsCustomer = " + _isCustomer +
                                                " AND IsVendor = " + _isVendor +
                                                " AND IsEmployess = " + _isEmployee,
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};

            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<AccountingObject> GetAccountingObjectsByPageAndBranch(int currentPage, string restaurantId, string accountingObjectCategoryId)
        {
            string procedures = "Get_Paged_AccountingObjects_ByBranch";

            object[] parms = {  "@WhereClause", " RestaurantId = '" + restaurantId + "'" +
                                                " AND AccountingObjectCategoryId = '" + accountingObjectCategoryId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};

            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<AccountingObject> GetAccountingObjectsActiveByBranch(bool isActive, string restaurantId, bool isCustomer, bool isVendor, bool isEmployee)
        {
            string procedures = "Get_All_AccountingObjects_ByCondition";
            int _isCustomer = 0;
            int _isVendor = 0;
            int _isEmployee = 0;
            int _isActive = 0;

            if (isCustomer == true) _isCustomer = 1;
            else _isCustomer = 0;

            if (isVendor == true) _isVendor = 1;
            else _isVendor = 0;

            if (isEmployee == true) _isEmployee = 1;
            else _isEmployee = 0;

            if (isActive == true) _isActive = 1;
            else _isActive = 0;

            object[] parms = {  "@WhereClause", " WHERE RestaurantId = '" + restaurantId + "'" +
                                                " AND IsCustomer = " + _isCustomer +
                                                " AND IsVendor = " + _isVendor +
                                                " AND IsEmployess = " + _isEmployee +
                                                " AND IsActive = " + _isActive };

            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_AccountingObject";

            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public int GetTotalPageByBranch(string restaurantId, bool isCustomer, bool isVendor, bool isEmployee)
        {
            string procedures = "Get_PageSize_AccountingObject";

            int _isCustomer = 0;
            int _isVendor = 0;
            int _isEmployee = 0;

            if (isCustomer == true) _isCustomer = 1;
            else _isCustomer = 0;

            if (isVendor == true) _isVendor = 1;
            else _isVendor = 0;

            if (isEmployee == true) _isEmployee = 1;
            else _isEmployee = 0;

            object[] parms = { "@WhereClause", "WHERE RestaurantId = '" + restaurantId + "'" +
                                                " AND IsCustomer = " + _isCustomer +
                                                " AND IsVendor = " + _isVendor +
                                                " AND IsEmployess = " + _isEmployee, };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public int GetTotalPage(bool isCustomer, bool isVendor, bool isEmployee)
        {
            string procedures = "Get_PageSize_AccountingObject_IsCustomer";

            object[] parms = { "@IsCustomer", isCustomer, 
                               "@IsVendor", isVendor,
                               "@IsEmployee", isEmployee};

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public int GetTotalPageByCategory(string accountingObjectCategoryId)
        {
            string procedures = "Get_PageSize_AccountingObject_ByCategoryId";

            object[] parms = { "@AccountingObjectCategoryId", accountingObjectCategoryId };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public AccountingObject GetAccountingObject(string restaurantId, string accountingObjectId)
        {
            string procedures = "Get_All_AccountingObject_ByBranch_AndId";

            object[] parms = { "@RestaurantId", restaurantId,
                                "@AccountingObjectID", accountingObjectId  };
            return Db.Read(procedures, true, Make, parms); ;
        }

        public int GetCountAcountingObjectsByCategory(string AccountingObjectId)
        {
            string procedures = "Proc_GetCount_AccountingObject_ByCategory";

            object[] parms = { "@AccountingObjectCategoryID", AccountingObjectId };

            return Db.Count(procedures, true, parms);
        }

        public void InsertAccountingObject(AccountingObject accountingObject)
        {
            string sql = "Insert_AccountingObject";

            accountingObject.AccountingObjectID = Db.Insert(sql, true, Take(accountingObject));
        }

        public void UpdateAccountingObject(AccountingObject accountingObject)
        {
            string sql = "Update_AccountingObject";

            Db.Update(sql, true, Take(accountingObject));
        }

        public void DeleteAccountingObject(AccountingObject accountingObject)
        {
            string sql = "Delete_AccountingObject";

            Db.Update(sql, true, Take(accountingObject.AccountingObjectID));
        }

        private object[] Take(AccountingObject accountingObject)
        {
            return new object[]  
            {
                "@AccountingObjectID", accountingObject.AccountingObjectID,
                "@AccountingObjectCode", accountingObject.AccountingObjectCode,
                "@AccountingObjectName", accountingObject.AccountingObjectName,
                "@AccountingObjectCategoryId", accountingObject.AccountingObjectCategoryId,
                "@RestaurantId", accountingObject.RestaurantId,
                "@Prefix", accountingObject.Prefix,
                "@Address", accountingObject.Address,
                "@Tel", accountingObject.Tel,
                "@Fax", accountingObject.Fax,
                "@EmailAddress", accountingObject.EmailAddress,
                "@Website", accountingObject.Website,
                "@CompanyTaxCode", accountingObject.CompanyTaxCode,
                "@BirthDate", accountingObject.BirthDate,
                "@FiredDate", accountingObject.FiredDate,
                "@Description", accountingObject.Description,
                "@ContactName", accountingObject.ContactName,
                "@JobCandidateName", accountingObject.JobCandidateName,
                "@ContactMobile", accountingObject.ContactMobile,
                "@ContactHomeTel", accountingObject.ContactHomeTel,
                "@ContactAddress", accountingObject.ContactAddress,
                "@IsPersonal", accountingObject.IsPersonal,
                "@IdentificationNumber", accountingObject.IdentificationNumber,
                "@IssueDate", accountingObject.IssueDate,
                "@IssueBy", accountingObject.IssueBy,
                "@IsVendor", accountingObject.IsVendor,
                "@IsCustomer", accountingObject.IsCustomer,
                "@IsEmployess", accountingObject.IsEmployess,
                "@CountryRegion", accountingObject.CountryRegion,
                "@Province", accountingObject.Province,
                "@District", accountingObject.District,
                "@Village", accountingObject.Village,
                "@IsActive", accountingObject.IsActive,
                "@ContactSex", accountingObject.ContactSex,
                "@ContactEmail", accountingObject.ContactEmail,
                "@DepartmentID", accountingObject.DepartmentID,
            };
        }

        private static Func<IDataReader, AccountingObject> Make = reader =>
            new AccountingObject
            {
                AccountingObjectID = reader["AccountingObjectID"].AsString(),
                AccountingObjectCode = reader["AccountingObjectCode"].AsString(),
                AccountingObjectName = reader["AccountingObjectName"].AsString(),
                AccountingObjectCategoryId = reader["AccountingObjectCategoryId"].AsString(),
                RestaurantId = reader["RestaurantId"].AsString(),
                Prefix = reader["Prefix"].AsString(),
                Address = reader["Address"].AsString(),
                Fax = reader["Fax"].AsString(),
                EmailAddress = reader["EmailAddress"].AsString(),
                Website = reader["Website"].AsString(),
                Tel = reader["Tel"].AsString(),
                CompanyTaxCode = reader["CompanyTaxCode"].AsString(),
                BirthDate = reader["BirthDate"].AsDateTime(),
                FiredDate = reader["FiredDate"].AsDateTime(),
                Description = reader["Description"].AsString(),
                ContactName = reader["ContactName"].AsString(),
                JobCandidateName = reader["JobCandidateName"].AsString(),
                ContactSex = reader["ContactSex"].AsBool(),
                ContactMobile = reader["ContactMobile"].AsString(),
                ContactEmail = reader["ContactEmail"].AsString(),
                ContactHomeTel = reader["ContactHomeTel"].AsString(),
                ContactAddress = reader["ContactAddress"].AsString(),
                IsPersonal = reader["IsPersonal"].AsBool(),
                IdentificationNumber = reader["IdentificationNumber"].AsString(),
                IssueDate = reader["IssueDate"].AsDateTime(),
                IssueBy = reader["IssueBy"].AsString(),
                IsVendor = reader["IsVendor"].AsBool(),
                IsCustomer = reader["IsCustomer"].AsBool(),
                IsEmployess = reader["IsEmployess"].AsBool(),
                CountryRegion = reader["CountryRegion"].AsString(),
                Province = reader["Province"].AsString(),
                District = reader["District"].AsString(),
                Village = reader["Village"].AsString(),
                IsActive = reader["IsActive"].AsBool(),
                DepartmentID = reader["DepartmentID"].AsString()
            };

        private object[] Take(string accountingObjectId)
        {
            return new object[]  
            {
                "@AccountingObjectID", accountingObjectId,
            };
        }
    }
}
